#include "Hostility.h"

	// empty constructor
	Hostility::Hostility()
	{
	}

	Hostility::Hostility(const std::string& entity_type)
	{
		loadFromFile(entity_type);
	}

	void Hostility::loadFromFile(const std::string& entity_type)
	{
		std::string real_path = "./baseq3/behaviours/" + entity_type + ".react";
		std::ifstream is(real_path.c_str(), std::ios_base::in);
		if (!is.is_open())
		{
			std::cerr << "***** WARNING: Could not load hostility list from file: " << real_path << std::endl;
			return;
		}

		while (!is.eof())
		{
			std::string type;
			std::string hostile;

			is >> type;
			is >> hostile;

			if (type != "" && hostile != "")
			{
				reaction_t react;
				if (hostile == "hostile")
					react = Hostility::HOSTILE;
				else if (hostile == "feared")
					react = Hostility::FEARED;
				else if (hostile == "neutral")
					react = Hostility::NEUTRAL;
				else if (hostile == "allied")
					react = Hostility::ALLIED;
				else
					react = Hostility::UNDEFINED;

				setReaction(type.c_str(), react);
			}
		}
		is.close();
	}

	// destructor
	Hostility::~Hostility()
	{

	}

	// set reaction to a type of creature
	void Hostility::setReaction(irr::core::string<c8> type,Hostility::reaction_t react)
	{
         if(getReaction(type) != Hostility::FEARED)
		                      hostilities__[type] = react;
	}

	// returns reaction to a type of creature
	Hostility::reaction_t Hostility::getReaction(irr::core::string<c8> type)
	{
		if(hostilities__.find(type) == hostilities__.end())
		{
			return Hostility::UNDEFINED ;
		}
		else
		{
			return hostilities__[type];
		}
	}

	// direct acess to a reaction by giving a type
	Hostility::reaction_t& Hostility::operator[](irr::core::string<c8> type)
	{
		return hostilities__[type] ;
	}

	// returns a reference to the whole list
	std::map<irr::core::string<c8>,Hostility::reaction_t>& Hostility::gethostilities()
	{
		return hostilities__ ;
	}	

// oprator overloading in order to display the attitudes
std::ostream& operator<<(std::ostream& os, Hostility::reaction_t r)
{
	std::string s;
	switch(r)
	{
		case Hostility::HOSTILE:
		s = "HOSTILE";
		break;

		case Hostility::FEARED:
		s = "FEARED";
		break;

		case Hostility::NEUTRAL:
		s = "NEUTRAL";
		break;

		case Hostility::ALLIED:
		s = "ALLIED";
		break;

		case Hostility::UNDEFINED:
		s = "UNDEFINED";
		break;
	}

	os << s;

	return os;
}

